<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属物业" prop="propertyId">
        <el-select v-model="queryParams.propertyId" placeholder="请选择物业" size="mini">
          <el-option v-for="property in propertyListOptions"
                     :key="property.id"
                     :label="property.propertyName"
                     :value="property.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="所属小区" prop="estateId">
        <el-select v-model="queryParams.estateId" placeholder="请选择小区" filterable clearable size="mini">
          <el-option v-for="estate in estatesOptions"
                     :key="estate.id"
                     :label="estate.estateName"
                     :value="estate.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="缴费方式" prop="rechargeWay">
        <el-select v-model="queryParams.rechargeWay" placeholder="请选择缴费方式" filterable clearable size="mini">
          <el-option label="现金充值" :value="1" />
          <el-option label="扫码充值" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="户号" prop="roomNo">
        <el-input v-model="queryParams.roomNo" placeholder="请输入业主户号" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="查询时间" prop="beginTime">
        <el-date-picker v-model="dateRange" clearable size="mini" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
                   v-hasPermi="['module:accountRecord:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="充值" name="0"/>
      <el-tab-pane label="消费" name="1"/>
    </el-tabs>
    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="所属物业" align="center" prop="propertyName"/>
      <el-table-column label="所属小区" align="center" prop="estateName"/>
      <el-table-column label="房间号" align="center" prop="roomNo"/>
      <el-table-column label="余额" align="center" prop="account">
        <template slot-scope="scope">
          {{scope.row.account+"元"}}
        </template>
      </el-table-column>
      <el-table-column label="充值金额" align="center" prop="usageAmount" v-if="activeName==0">
        <template slot-scope="scope">
          {{scope.row.usageAmount+"元"}}
        </template>
      </el-table-column>
      <el-table-column label="消费金额" align="center" prop="usageAmount" v-if="activeName==1">
        <template slot-scope="scope">
          {{scope.row.usageAmount+"元"}}
        </template>
      </el-table-column>
      <el-table-column label="充值方式" align="center" prop="rechargeWay"  v-if="activeName==0">
        <template slot-scope="scope">
          <div>
            <el-tag v-if="scope.row.rechargeWay === 0" style="cursor: pointer" :type="'danger'">消费</el-tag>
            <el-tag v-if="scope.row.rechargeWay === 1" style="cursor: pointer" :type="'success'">现金充值</el-tag>
            <el-tag v-if="scope.row.rechargeWay === 2" style="cursor: pointer" :type="''">微信充值</el-tag>
            <el-tag v-if="scope.row.rechargeWay === 3" style="cursor: pointer" :type="''">刷卡充值</el-tag>
            <el-tag v-if="scope.row.rechargeWay === 4" style="cursor: pointer" :type="''">支付宝充值</el-tag>
            <el-tag v-if="scope.row.rechargeWay === 6" style="cursor: pointer" :type="''">转账支付</el-tag>
            <el-tag v-if="scope.row.rechargeWay === 7" style="cursor: pointer" :type="''">多种付款</el-tag>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="消费方式" align="center" prop="rechargeWay"  v-if="activeName==1">
        <template slot-scope="scope">
          <div>
            <el-tag v-if="scope.row.rechargeWay === 0" style="cursor: pointer" :type="'danger'">消费</el-tag>
            <el-tag v-if="scope.row.rechargeWay === 1" style="cursor: pointer" :type="'success'">现金消费</el-tag>
            <el-tag v-if="scope.row.rechargeWay === 2" style="cursor: pointer" :type="''">微信消费</el-tag>
            <el-tag v-if="scope.row.rechargeWay === 3" style="cursor: pointer" :type="''">刷卡消费</el-tag>
            <el-tag v-if="scope.row.rechargeWay === 4" style="cursor: pointer" :type="''">支付宝消费</el-tag>
            <el-tag v-if="scope.row.rechargeWay === 5" style="cursor: pointer" :type="''">抵扣消费</el-tag>
            <el-tag v-if="scope.row.rechargeWay === 6" style="cursor: pointer" :type="''">转账消费</el-tag>
            <el-tag v-if="scope.row.rechargeWay === 7" style="cursor: pointer" :type="''">多种消费</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作时间" align="center" prop="createTime"/>
      <el-table-column label="操作人" align="center" prop="sysUserName"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"-->
<!--                     v-hasPermi="['module:accountRecord:edit']">修改-->
<!--          </el-button>-->
<!--          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"-->
<!--                     v-hasPermi="['module:accountRecord:remove']">删除-->
<!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
  </div>
</template>

<script>
import {
  listRecord,
  getRecord,
  delRecord,
  addRecord,
  updateRecord,
  exportRecord
} from "@/api/module/estate/accountRecord";
import {listPropertyNoPage} from "@/api/module/property/property";
import {listAllEstate} from "@/api/module/estate/estate";
import Template from "@/views/module/examine/template";

export default {
  name: "Record",
  components: {Template},
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      activeName:'0',
      dateRange: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小区房屋套内账户充值/消费记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 使用时间时间范围
      daterangeCreateTime: [],
      propertyListOptions: [],
      estatesOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rechargeOrUse:0,
        propertyId:undefined,
        estateId:undefined,
        roomNo:undefined,
        beginDate:undefined,
        endDate:undefined,
        rechargeWay:undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList();
    this.getPropertyList();
    this.getEstateList();
  },
  methods: {
    /** 查询小区房屋套内账户充值/消费记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getPropertyList() {
      listPropertyNoPage().then(response => {
        this.propertyListOptions = response.data;
      });
    },
    handleClick(val){
      console.log('handleClick',val.name);
      this.activeName = val.name;
      this.queryParams.rechargeOrUse = parseInt(val.name);
      this.getList();
    },
    getEstateList() {
      listAllEstate().then(response => {
        this.estatesOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        account: undefined,
        usageAmount: undefined,
        createTime: undefined,
        sysUserId: undefined,
        rechargeOrUse: undefined,
        rechargeWay: undefined,
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.dateRange != undefined && this.dateRange.length === 2) {
        this.queryParams.beginDate = this.dateRange[0];
        this.queryParams.endDate = this.dateRange[1];
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加小区房屋套内账户充值/消费记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getRecord(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改小区房屋套内账户充值/消费记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addRecord(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除小区房屋套内账户充值/消费记录编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delRecord(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有小区房屋套内账户充值/消费记录数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportRecord(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
